<template>
  <!-- 新增参数集合 表单 -->
  <a-modal
    v-model:open="visible"
    :title="isEdit ? '编辑参数集合' : '新增参数集合'"
    @ok="handleOk"
    class="addStation"
    destroyOnClose
    centered
    :width="760"
    @cancel="cancel"
  >
    <a-form
      :model="form"
      :rules="rules"
      layout="inline"
      :labelCol="labelCol"
      ref="formRef"
    >
      <a-form-item label="参数集合名称：" name="name">
        <a-input
          ref="select"
          v-model:value="form.name"
          class="formInput"
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="参数集合编码：" name="code">
        <a-input
          v-model:value="form.code"
          class="formInput"
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="参数集合备注：">
        <a-input
          v-model:value="form.remark"
          class="formInput"
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="绑定参数：" name="paramCodeIds">
        <a-select
          v-model:value="form.paramCodeIds"
          show-search
          placeholder="请搜索"
          style="width: 200px"
          :show-arrow="false"
          class="formInput"
          mode="multiple"
          allowClear
          optionFilterProp="label"
        >
          <a-select-option v-for="item in paramCodeList" :value="item.id" :key="item.id" :label="item.name">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
    <template #footer>
      <div class="footerBar">
        <a-button key="back" @click="cancel">取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确认</a-button
        >
      </div>
    </template>
  </a-modal>
</template>
<script setup>


import { debounce, rest } from "lodash-es";
const $api = inject("$api");
const props = defineProps({
  addParamVisible: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
  },
  isEdit: {
    type: Boolean,
    default: false,
  },
});
const visible = ref(props.addParamVisible);
const loading = ref(false);

const emits = defineEmits([
  "update:addParamVisible",
  "getParamCodeCollectPage",
]);
watch(
  () => props.addParamVisible,
  (val) => {
    visible.value = val;
    getParamCodeList();
    if (val && props.isEdit) {
      form.value = {
        ...props.rowData,
      };
    }
  }
);
const labelCol = { style: { width: "120px" } };
const formRef = ref(null);
const form = ref({
  name: undefined,
});
const rules = ref({
  name: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  code: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  paramCodeIds: [
    {
      required: true,
      message: "请输入",
      trigger: "change",
    },
  ],
});
const cancel = () => {
  emits("update:addParamVisible", false);
  form.value = {
    name: undefined,
    code: undefined,
    remark: undefined,
  };
  formRef.value.clearValidate();
};
const handleSubmit = () => {
  formRef.value
    .validate()
    .then(async () => {
      const {paramCodeList, ...rest} = form.value
      const params = {
        ...rest,
      };
      if (props.isEdit) {
        // 编辑带上id
        params.id = props.rowData.id;
      }
      loading.value = true;
      const res = await $api.paramCodeCollect(
        params,
        props.isEdit ? "put" : "post"
      );
      loading.value = false;
      if (res.code === 0) {
        message.success(props.isEdit ? "编辑成功" : "新增成功");
        cancel();
        emits("getParamCodeCollectPage");
      }
    })
    .catch((error) => {
      console.log("error", error);
    });
};
const searchList = ref([]);
const handleSearch = async (name) => {
  const res = await $api.paramCodeList({ name });
  searchList.value = res.data.map((item) => {
    return {
      label: item.name,
      value: item.id,
    };
  });
};
const paramCodeList = ref([]);
const getParamCodeList = async () => {
  const res = await $api.paramCodeList();
  if (res && res.code === 0) {
    paramCodeList.value = res.data;
  }
};
const debouncedSearch = debounce(handleSearch, 300);
</script>

<style lang="less" scoped>
.form {
  padding: 0 40px;
  margin-top: 30px;
}
.formInput,
.formSelect {
  width: 220px !important;
}
.ant-form-item {
  margin-top: 20px;
}
.footerBar {
  text-align: center;
  margin-top: 30px;
}
</style>